﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace OnTheWay.Domain
{
    public class DomainTransfer<T,TIService> : DomainBase<T,TIService>
    {
        /// <summary>
        /// 执行自定义方法
        /// </summary>
        /// <param name="MothedName">方法名</param>
        /// <param name="parameters">方法参数</param>
        /// <param name="isAbsolutely">是否使用绝对 方法名</param>
        /// <returns></returns>
        public ServiceTransfer InvokeTransfer(string MothedName, object[] parameters, bool isAbsolutely = false)
        {
            string value = Invoke(MothedName, parameters, isAbsolutely).ToString();
            if (value == null || value.Length == 0)
            {
                throw new Exception("服务器接口["+_tName + MothedName + "]返回空！");
            }
            ServiceTransfer transfer = JsonConvert.DeserializeObject<ServiceTransfer>(value);
            if (transfer.Exception != null && transfer.Exception.Length > 0)
            {
                throw new Exception(transfer.DecodeException);
            }
            return transfer;
        }

        /// <summary>
        /// 执行自定义方法
        /// </summary>
        /// <param name="MothedName">方法名</param>
        /// <param name="parameters">方法参数</param>
        /// <param name="isAbsolutely">是否使用绝对 方法名</param>
        /// <returns></returns>
        public TT InvokeTransfer<TT>(string MothedName, object[] parameters, bool isAbsolutely = false)
        {
            return InvokeTransfer(MothedName, parameters, isAbsolutely).GetReturn<TT>();
        }

        /// <summary>
        /// 执行自定义方法
        /// </summary>
        /// <param name="MothedName">方法名</param>
        /// <param name="parameters">方法参数</param>
        /// <param name="isAbsolutely">是否使用绝对 方法名</param>
        /// <returns></returns>
        public IList<T> InvokeTransferList(string MothedName, object[] parameters, bool isAbsolutely = false)
        {
            return InvokeTransfer(MothedName, parameters, isAbsolutely).GetReturnList<T>();
        }
    }
}
